#_*_coding:utf-8_*_

def centos7(raw_data):
    mac_list = []
    tmp = []
    data = dict()
    for line in raw_data:
        if "ether" in line:
            mac_addr = line.split()[1]
            ip_addr=None
            mask_addr = None
            ip_addr2 = None
            mask_addr2 = None
            for item in tmp:
                if "BROADCAST" in item:
                    nic_name = item.split(":")[0]  # get name
                if "broadcast" in item:
                    _,ip_addr,_,mask_addr,_,_ = item.split()
            if mac_addr not in data:
                if ip_addr2:
                    data[mac_addr] = {'name': nic_name,
                            'macaddress': mac_addr,
                            'netmask': mask_addr,
                            'network': ip_addr,
                            'bonding': 0,
                            'model': 'unknown',
                            'ipaddress': ip_addr,
                            }
                else:
                    data[mac_addr] = {'name': nic_name,
                            'macaddress': mac_addr,
                            'netmask': mask_addr,
                            'network': ip_addr,
                            'bonding': 0,
                            'model': 'unknown',
                            'ipaddress': ip_addr,
                            }
            else:
                if '%s_bonding_addr' %(mac_addr) not in data:
                    random_mac_addr = '%s_bonding_addr' %(mac_addr)
                    print random_mac_addr
                else:
                    random_mac_addr = '%s_bonding_addr2' %(mac_addr)

                data[random_mac_addr] = {'name': nic_name,
                        'macaddress':random_mac_addr,
                        'netmask': mac_addr,
                        'network': ip_addr,
                        'bonding': 1,
                        'model': 'unknown',
                        'ipaddress': ip_addr,
                        }
            mac_list.append(data)
        else:
            tmp.append(line)

    nic_list= []
    for k,v in data.items():
        nic_list.append(v)
    # print nic_list
    return nic_list


def centos6(raw_data):
    nic_dic = {}
    next_ip_line = False
    last_mac_addr = None
    for line in raw_data:
        if next_ip_line:
            # print last_mac_addr
            # print line #, last_mac_addr.strip()
            next_ip_line = False
            nic_name = last_mac_addr.split()[0]
            if "tun" in nic_name:
                raw_bcast = line.split("P-t-P:")
            else:
                raw_bcast = line.split("Bcast:")
            mac_addr = last_mac_addr.split("HWaddr")[1].strip()
            raw_ip_addr = line.split("inet addr:")
            raw_netmask = line.split("Mask:")
            if len(raw_ip_addr) > 1:  # has addr
                ip_addr = raw_ip_addr[1].split()[0]
                network = raw_bcast[1].split()[0]
                netmask = raw_netmask[1].split()[0]
                # print(ip_addr,network,netmask)
            else:
                ip_addr = None
                network = None
                netmask = None
            if mac_addr not in nic_dic:
                nic_dic[mac_addr] = {'name': nic_name,
                                     'macaddress': mac_addr,
                                     'netmask': netmask,
                                     'network': network,
                                     'bonding': 0,
                                     'model': 'unknown',
                                     'ipaddress': ip_addr,
                                     }
            else:  # mac already exist , must be boding address
                if '%s_bonding_addr' % (mac_addr) not in nic_dic:
                    random_mac_addr = '%s_bonding_addr' % (mac_addr)
                else:
                    random_mac_addr = '%s_bonding_addr2' % (mac_addr)

                nic_dic[random_mac_addr] = {'name': nic_name,
                                            'macaddress': random_mac_addr,
                                            'netmask': netmask,
                                            'network': network,
                                            'bonding': 1,
                                            'model': 'unknown',
                                            'ipaddress': ip_addr,
                                            }
        if "HWaddr" in line:
            # print line
            next_ip_line = True
            last_mac_addr = line

    nic_list = []
    for k, v in nic_dic.items():
        nic_list.append(v)

    # print nic_list
    return nic_list